//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    total :0,
    _num  : 0,
    iscode: 0,
    city  : "",
    month : 7,
    uploadUrl : app.uploadUrl,
    num: 1,
    show:0,
    logo: false,
    canIUse : wx.canIUse('button.open-type.getUserInfo'),
    userback:[
      {
        address : "西安用户",
        name    : "超有钱",
        money   : "返现1000",
      }
    ],
    sellers:[]
  },
  search: function () {
    wx.navigateTo({
      url: '../search/search',
    })
  },
  sendRequest: function (res) {
    let that = this;
    if (!res.detail.rawData || !wx.getStorageSync('uid')) {
      wx.showModal({
        title: '提示',
        content: '请重新点击授权',
      })
      return false;
    }
    wx.showLoading({
      title: '请稍后',
    })
    var info = res.detail.userInfo;
    wx.request({
      url: app.url+'/user/defaults',
      data:{
        uid    : wx.getStorageSync('uid'),
        wxname : info.nickName,
        sex    : info.gender,
        avatar : info.avatarUrl
      },
      method:'post',
      success:function(res){
        if(res.data.code==1){
          wx.setStorageSync('userinfo', res.data.data);
          that.setData({ logo:false })
          wx.hideLoading();
        }else{
          wx.hideLoading();
          wx.showToast({
            title: '获取失败请重试',
            icon:'none'
          })
        }
      }
    })
  },
  //分享
  onShareAppMessage: function (res) {
    var uid = wx.getStorageSync('uid');
    if (res.from === 'button') {

    }
    return {
      title: '自定义转发标题',
      path: '/pages/index/index?uid=' + uid,
      success: function (res) {
        wx.showShareMenu({      
          withShareTicket: true
        });
      },
      fail: function (res) {
      }
    }
  },
  chose_city:function(){
    wx.navigateTo({
      url: '../address/address',
    })
  },
  collect: function (e) {
    var bid     = e.currentTarget.dataset.id;
    let that    = this;
    var iscode  = that.data.collect;
    var sellers = that.data.sellers;
    var index   = e.currentTarget.dataset.index
    wx.request({
      url    : app.url + '/Business/add_collect',
      data   : {
        uid  : wx.getStorageSync('uid'),
        bid  : bid,
        type : 0
      },
      method  : 'post',
      success : function (res) {
        var code = res.data.res.code;
        if (code == 1){
          wx.showToast({title: '收藏成功',})
          sellers[index].collect =true;
        }else if (code == 2){
          wx.showToast({title: '取消收藏',})
          sellers[index].collect = false;
        }else{
          wx.showToast({title: '操作失败',})
        }
        that.setData({
          sellers: sellers
        })
      }
    })
  },
  newsurl:function(){
    wx.navigateTo({
      url: '../news/news',
    })
  },
  goto:function(){
    wx.navigateTo({
      url: '../map/map',
    })
  },
  start_ads: function (res) {
    var location = res.currentTarget.dataset;
    wx.openLocation({
      latitude : location.lat,
      longitude: location.lng,
      scale    : 18,
      name     : location.name,
      address  : location.address
    })
  },
  goshop:function(e){
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../store/store?bid=' + id,
    })
  },
  recordjl:function(){
    wx.navigateTo({ url: '../recordjl/recordjl'})
  },
  nb_clickurl1: function(e){ wx.navigateTo({ url: '../refund/refund'})},
  nb_clickurl2: function(e){ wx.navigateTo({ url: '../nearby/nearby'})},
  nb_clickurl3: function(e){ wx.redirectTo({ url: '../mall/mall' })},
  nb_clickurl4: function(e){ wx.navigateTo({ url: '../aboutUs/aboutUs'})},
  focus_click:function(e){
    wx.showLoading({ title: 'loading..'})
    let that = this
    that.setData({_num:e.currentTarget.dataset.num })
    wx.getLocation({
      type: 'gcj02',
      success: function(res) {
        that.get_business(app.data.address.name, res.longitude, res.latitude);
      },
    })
  },
  onLoad: function () {
    let that     = this;
    if(!wx.getStorageSync('userinfo'))that.setData({logo:true})
    var index    = 0;
    var userback = that.data.userback;
    var num_     = that.data.num;
    var interval = setInterval(function(){
      index      = index + 1 ;
      num_       = num_ + 1
      that.setData({
        num : num_
      })
      if(index == userback.length){
        that.setData({
          num : 1
        })
        index = 0 ;
        num_ = 1
      }  
    },1500)
  },
  mall  : function(){ wx.redirectTo({ url: "../mall/mall"})},
  issue : function(){ wx.navigateTo({ url: "../issue/issue"})},
  car   : function(){ wx.redirectTo({ url: "../car/car" })},
  mine  : function(){ wx.redirectTo({ url: "../mine/mine"})},
  onShow: function () {
    let that = this
    that.get_city() ;
    that.get_news() ;
    that.get_reback();
    wx.request({
      url: app.url + '/Reback/total_num',
      method: 'post',
      success: function (res) {
        that.setData({
          total: res.data.data
        })
      }
    });
    clearInterval(app.data.set)
    app.data.set =setInterval(function () {
                    wx.request({
                      url: app.url + '/Reback/total_num',
                      method: 'post',
                      success: function (res) {
                        if (res){
                          that.setData({
                            total: res.data.data
                          })
                        }
                      }
                    })
                  }, 60000)  
  },
  get_news(){
    var that=this;
    wx.request({
      url    : app.url + '/Article/details',
      method : 'post',
      success: function (res) {
        that.setData({
          news : res.data.data['title'] ? res.data.data['title']:''
        })
      }
    })
  },
  get_city : function(){
    let that = this
    wx.getLocation({
      type    : 'gcj02',
      success : function (rs) {
    if (!app.data.address.name) {
          wx.request({
            url    : app.url + '/user/get_city',
            method : 'post',
            data   : {
              longitude : rs.longitude,
              latitude  : rs.latitude
            },
            success: function (res) {
              app.data.address.name = res.data.city,
                that.setData({
                  city : res.data.city
                })
              that.get_business(res.data.city, rs.longitude, rs.latitude);
            }
          })
    } else {
      that.setData({
        city : app.data.address.name
      })
      that.get_business(app.data.address.name, rs.longitude, rs.latitude);
    }
      },
    })
  },
  get_business: function (city, lng,lat){
    var that=this;
    wx.request({
      url    : app.url +'/Business/_list',
      method : 'post',
      data:{
        city : city,
        lng  : lng,
        lat  : lat,
        order: that.data._num,
        uid  : wx.getStorageSync('uid')
      },
      success:function(res){
        that.setData({
          sellers : res.data.data
        })
        wx.hideLoading()
      }
    }) 
  },
  get_reback : function(){
    var that = this ;
    wx.request({
      url     : app.url + '/Reback/user_list',
      method  : 'post',
      data    : {reback : 'user'},
      success : function (res) {
        if (res.data.code == 1){
          that.setData({
            userback: res.data.data
          })
        }
      }
    }) 
  },
  onHide:function(){
    clearInterval(this.data.set)
  },
  onReady: function () {
  
  },
  onShareAppMessage: function () {

  },
})
